import store from '@/store'
import router from '@/router'
import { getMenuByPath } from "@/router/routerUtil";
import { ElMessage } from 'element-plus'

/**
 * 打开一个页面
 * @param {页面路径} path 
 */
export const openPage = (path, params)=>{
    let menus = store.getters['menu/getMenus'];
    let menu = getMenuByPath(menus, path);
    if(!menu) {
        menu = getMenuByPath(menus, '/404');
    }
    if(menu) {
        menu.params = params;
        if(!menu.jumpType) {
            store.dispatch('tabs/openPage', menu);
            router.push({ 
                path: menu.path, 
                query: params ? params : {}
            });
        }else if(menu.jumpType == 1) {
            window.open(menu.jumpUrl, '_blank');
        }
    }else {
        ElMessage.error('页面不存在');
    }
}

/**
 * 关闭一个页面
 * @param {页面路径} path 
 */
export const removePage = (path)=>{
    store.dispatch('tabs/removePage', path);
}